﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common.Logging;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;

public partial class index : System.Web.UI.Page
{
    private PostFactory m_PostFactory;
    private IPostService m_PostService;

    //最新消息m_NewsNodeId=9
    private int m_NewsNodeId = 9;
    //首頁廣告m_AdsNodeId=8
    private int m_AdsNodeId = 8;
    //商品分類NodeId=3
    private int m_ProductNodeId = 3;

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();

        if (!IsPostBack)
        {
            fillRepeaterNews();
            fillRepeaterAds();
            fillRepeaterProductNodes();
        }
    }

    private void fillRepeaterProductNodes()
    {
        IList<NodeVO> nodeList = m_PostService.GetNodeListByParentId(m_ProductNodeId);
        if (nodeList != null && nodeList.Count > 0)
        {
            RepeaterProducts.DataSource = nodeList.OrderBy(p => p.SortNo);
            RepeaterProducts.DataBind();
        }
    }

    private void fillRepeaterAds()
    {
        //搜尋條件
        DateTime? startDate = null;
        string sortField = "SortNo";
        bool sortDesc = true;

        IList<PostVO> list = m_PostService.GetPostListByNodeId(m_AdsNodeId, true, startDate, 0, 5, sortField, sortDesc);
        RepeaterAds.DataSource = list;
        RepeaterAds.DataBind();

        ////設定第一張
        if (list != null && list.Count > 0)
        {
            ltlDivAds.Text = string.Format("<div id='adv-pp' style='background-image:url(upload/{0})'></div>", list[0].PicFileName);
        }
    }

    private void fillRepeaterNews()
    {
        //搜尋條件
        DateTime? startDate = DateTime.Today;
        string sortField = "ShowDate";
        bool sortDesc = true;

        RepeaterNews.DataSource = m_PostService.GetPostListByNodeId(m_NewsNodeId, true, startDate, 0, 3, sortField, sortDesc);
        RepeaterNews.DataBind();
    }

    protected void RepeaterNews_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            Control ctrl = e.Item;
            PostVO postVO = (PostVO)e.Item.DataItem;

            //內容
            int titleLength = 25;
            if (!string.IsNullOrEmpty(postVO.Title))
            {
                string title = postVO.Title;
                if (title.Length > titleLength)
                {
                    title = title.Substring(0, titleLength) + "...";
                }

                UIHelper.SetLabelText(ref ctrl, "lblTitle", title);
            }
        }
    }
}